package cn.itmantzy.product.service.impl;

import cn.itmantzy.basic.service.impl.BaseServiceImpl;
import cn.itmantzy.pet.domain.Pet;
import cn.itmantzy.pet.mapper.PetDetailMapper;
import cn.itmantzy.product.domain.Product;
import cn.itmantzy.product.domain.ProductDetail;
import cn.itmantzy.product.mapper.ProductMapper;
import cn.itmantzy.product.service.IProductDetailService;
import cn.itmantzy.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private IProductDetailService productDetailService;
    @Override
    public void add(Product product){
        // 1.应该先保存product，返回自增id
        productMapper.save(product); // 得到自增id
        // 2.获取productDetail
        ProductDetail productDetail = product.getProductDetail();
        // 3.把获取的product自增id，赋值给productDetail
        productDetail.setProduct_id(product.getId());
        // 4.如果productDetail不为空则调用save方法保存detail信息
        if(productDetail != null){
            productDetailService.add(productDetail);
        }
    }
    @Override
    public void remove(Long id){
        Product product = productMapper.loadById(id);
        if(product != null){
            productDetailService.removeByProductId(product.getId());
        }
        productMapper.delete(id);
    }

    @Override
    public void onSale(List<Long> ids) {
        Map<String, Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("onsaletime", new Date());
        productMapper.onSale(map);
    }

    @Override
    public void offSale(List<Long> ids) {
        Map<String, Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("offsaletime", new Date());
        productMapper.offSale(map);
    }

    @Override
    public Product queryByIdDetail(Long id) {
        Product product = productMapper.loadByIdDetail(id);
        return product;
    }
}
